#include "ADUDuck.h"
#include "ADUConstant.h"
#include "ADUGameManager.h"

USING_NS_CC;

Duck::Duck() {

}

Duck::~Duck() {

}

Duck* Duck::create(const float& x, const float& y,const string& file) {
	
	Duck* pSprite = new Duck();
	if (pSprite && pSprite->initWithFile(file)) {
		pSprite->autorelease();
		pSprite->setPosition(x, y);
		pSprite->initialState();
		pSprite->createBody(x,y);
		log("Created duck");
		return pSprite;
	}

	CC_SAFE_DELETE(pSprite);

	return NULL;
}

void Duck::initialState() {
	this->setState(DUCK_IDLE);
	this->setHealthStatus(DUCK_HEALTH);
}

void Duck::setState(const int& _state) {
	m_state = _state;
}

int Duck::getState() const {
	return m_state;
}

void Duck::createBody(float x, float y) {

	b2BodyDef bodyDef;
	bodyDef.type = b2_dynamicBody;
	bodyDef.position.Set(x/PTM_RATIO, y/PTM_RATIO);
	bodyDef.userData = this;

//	b2PolygonShape shape;
	b2CircleShape shape;
	shape.m_radius = this->getContentSize().width / PTM_RATIO;
//	float spriteWidth = this->getContentSize().width;
//	float spriteHeight = this->getContentSize().height;
//	shape.SetAsBox(spriteWidth/PTM_RATIO, spriteHeight/PTM_RATIO);

	b2FixtureDef fixtureDef;
	fixtureDef.shape = &shape;
	fixtureDef.density = 1.0f;
	fixtureDef.friction = 0.0f;
	fixtureDef.restitution = 1.0f;

	m_duckBody = (GameManager::getInstance()->mWorld)->CreateBody(&bodyDef);
	m_duckFixture = m_duckBody->CreateFixture(&fixtureDef);
}

void Duck::changeTextureByState(const int& _state) {
	switch (_state) {
	case DUCK_IDLE:
		this->setTexture(IDLE_STATE_IMG);
		break;
	case DUCK_FLYING:
		this->setTexture(FLYING_STATE_IMG);
		break;
	default:
		break;
	}
}

void Duck::setHealthStatus(const int& _currentHealth) {
	m_health = _currentHealth;
}
int Duck::getHealthStatus() const {
	return m_health;
}

void Duck::update(float dt) {
	
}
